<template>
  <div>
    <el-row>
      <el-col :span="3"
              class="grid">
        <el-input v-model="intxt"
                  placeholder="请输入内容"
                  size="mini"
                  width="180"></el-input>
      </el-col>
      <el-col :span="1"
              class="grid">
        <el-button type=" text"
                   icon="el-icon-search"
                   size="mini">搜索</el-button>
      </el-col>
    </el-row>
    <br />
    <!--表格数据及操作-->
    <el-table :data="housing"
              border
              style="width: 100%"
              stripe
              ref="multipleTable"
              tooltip-effect="dark">
      <!--勾选框-->
      <el-table-column type="selection"
                       width="55"
                       label="序号">
      </el-table-column>
      <!--索引-->
      <el-table-column label="序号"
                       type="index"> </el-table-column>
      <el-table-column prop="id"
                       label="编号"
                       width="90"
                       sortable>
      </el-table-column>
      <el-table-column prop="phone"
                       label="电话号码"
                       width="140"
                       sortable>
      </el-table-column>
      <el-table-column prop="householder"
                       label="户主"
                       width="90"
                       sortable>
      </el-table-column>
      <el-table-column prop="identityCard"
                       label="身份证号码"
                       width="180"
                       sortable>
      </el-table-column>
      <el-table-column prop="houseNumber"
                       width="120"
                       label="房屋门牌号"> </el-table-column>
      <el-table-column prop="water"
                       label="水费"
                       width="90"
                       sortable>
      </el-table-column>
      <el-table-column prop="power"
                       width="90"
                       label="电费"> </el-table-column>
      <el-table-column width="120"
                       label="操作">
                         <template slot-scope="scope">
        <el-button @click="transfer(scope.$index)">过户</el-button>
      </template>
      </el-table-column>
    </el-table>
    <el-dialog title="房屋过户"
               :visible="Transfer"
               size="tiny"
               :modal-append-to-body="false"
               @close="closeDialog">
      <el-form>
        <el-form-item label="编号"
                      prop="id">
          <el-input v-model="addForm.id"
                    :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="门牌号"
                      prop="houseNumber">
          <el-input v-model="addForm.houseNumber"
                    :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="户主"
                      prop="householder">
          <el-input v-model="addForm.householder"></el-input>
        </el-form-item>
        <el-form-item label="身份证号码"
                      prop="identityCard">
          <el-input v-model="addForm.identityCard"></el-input>
        </el-form-item>
        <el-form-item label="电话"
                      prop="phone">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
      </el-form>
      <el-form id="#addForm"
               ref="addForm"
               :model="addForm"
               label-width="80px">

        <el-form-item>
          <el-button type="primary"
                     @click="productAddpower()">确定</el-button>
          <el-button @click="
              addPower = false;
              canceladdpowerT();
            ">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
import myapi from '../api.js'
export default {
  name: 'HousingSystem',
  data() {
    return {
      intxt: '',
      housing: [],
      Transfer: false,
      addForm: {
        id:0,
        houseNumber: '',
        householder: '',
        identityCard: '',
        phone: '',
      },
    }
  },
  mounted() {
     
    axios.post(myapi.housing.url).then((res) => {
      if (res.data){ this.housing = res.data
       
      }
      else {
        this.$message.error('查询失败')
      }
    })
  },
  methods: {
    transfer(index) {
    this.addForm.id = this.housing[index].id
      this.addForm.houseNumber = this.housing[index].houseNumber
      this.Transfer = true
      
    },
    canceladdpowerT() {
      this.Transfer = false
    },
    closeDialog() {
      this.Transfer = false
    },
    productAddpower() {
      if (
        this.addForm.householder.length <= 0 ||
        this.addForm.identityCard.length <= 0 ||
        this.addForm.phone.length <= 0
      ) {
        this.$message({
          showClose: true,
          message: '过户失败，过户信息不能为空',
          type: 'error',
          center: true,
        })
        this.Transfer = false
        this.addForm.householder = ''
        this.addForm.identityCard = ''
        this.addForm.phone = ''
        return
      }
      axios
        .put(myapi.updateByHouseRevise.url, {
          houseNumber: this.addForm.houseNumber,
          householder: this.addForm.householder,
          identityCard: this.addForm.identityCard,
          phone: this.addForm.phone,
        })
        .then((res) => {
          if (res.data != null) {
            this.$message({
              showClose: true,
              message: '过户成功',
              type: 'success',
              center: true,
            })
            this.Transfer = false
            this.addForm.householder = ''
            this.addForm.identityCard = ''
            this.addForm.phone = ''
            this.chaxun()
          } else if (res.data === null) {
            this.Transfer = false
            this.addForm.householder = ''
            this.addForm.identityCard = ''
            this.addForm.phone = ''
            this.$message({
              showClose: true,
              message: '过户失败',
              type: 'error',
              center: true,
            })
          }
        })
    },
    chaxun(){
      axios.post(myapi.housing.url).then((res) => {
      if (res.data){ this.housing = res.data
       
      }
      else {
        this.$message.error('查询失败')
      }
    })
    }
  },
}
</script>

<style>
.div {
  margin: 50px auto;
}
.login {
  /* margin-top: 8%; */
  width: 50px;
  margin-left: auto;
  margin-right: auto;
  text-align: center;
}
</style>